/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import entity.EventInfo;
import entity.Registration;
import entity.UserInfo;
import entity.Winner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;

import javax.faces.bean.ManagedBean;


import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import model.EventInfoFacade;
import model.RegistrationFacade;
import model.UserInfoFacade;
import model.WinnerFacade;

/**
 *
 * @author VietTien
 */
@ManagedBean
@SessionScoped
public class WinnerController {

    @EJB
    private RegistrationFacade registrationFacade;
    @EJB
    private UserInfoFacade userInfoFacade;
    @EJB
    private EventInfoFacade eventInfoFacade;
    @EJB
    private WinnerFacade winnerFacade;
    private List<Winner> newestWinner;
    private Winner current;//Select current row
    private DataModel items = null;//Lay list event
    private List<Winner> eventList;
    private String eventID;
    private String playerID;
    private Map listWinnerTemp = new HashMap<String, String>();
    private String newPrize;
    private String editPrize;
    private  String editPlayerID;
    /** Creates a new instance of WinnerController */
    public WinnerController() {
    }

    public String getNewPrize() {
        return newPrize;
    }

    public String getEditPlayerID() {
        return editPlayerID;
    }

    public void setEditPlayerID(String editPlayerID) {
        this.editPlayerID = editPlayerID;
    }

    public String getEditPrize() {
        return editPrize;
    }

    public void setEditPrize(String editPrize) {
        this.editPrize = editPrize;
    }

    public Map getListWinnerEdit() {
        Map listWinnerEdit = new HashMap<String, String>();
        List<UserInfo> users = userInfoFacade.getUserByEventID(getSelected().getRegistration().getEventInfo().getEventID().toString());
        if (null != users) {
            for (UserInfo userInfo : users) {
                listWinnerEdit.put(userInfo.getUserName(), userInfo.getUserID());
            }
        } else {
            JsfUtil.addErrorMessage("This Event haven't any player !");
        }
        return listWinnerEdit;
    }

    public List<Winner> getNewestWinner() {
        if (newestWinner == null) {
            newestWinner = winnerFacade.getNewestWinner();
        }
        return newestWinner;
    }

    public void setNewestWinner(List<Winner> newestWinner) {
        this.newestWinner = newestWinner;
    }

    public void setNewPrize(String newPrize) {
        this.newPrize = newPrize;
    }

    public String getPlayerID() {
        return playerID;
    }

    public void setPlayerID(String playerID) {
        this.playerID = playerID;
    }

    public DataModel getItems() {
        if (items == null) {
            items = new ListDataModel(winnerFacade.findAll());
        }
        return items;
    }

    public String prepareCreate() {
        current = new Winner();
        return "AddNewWinner";
    }

    public String prepareView() {
        current = (Winner) getItems().getRowData();
        return "/Winner/ViewWinner";
    }

    public Winner getSelected() {
        if (current == null) {
            current = new Winner();
        }
        return current;
    }

    public Map getListWinnerTemp() {
        return listWinnerTemp;
    }

    public void setListWinnerTemp(Map listWinnerTemp) {
        this.listWinnerTemp = listWinnerTemp;
    }

    public String getEventID() {
        return eventID;
    }

    public void setEventID(String eventID) {
        this.eventID = eventID;
    }

    public Map getListEvent() {
        Map listEvent = new HashMap<String, String>();
        List<EventInfo> eventCompleted = eventInfoFacade.showCompletedEvent();
        if (null != eventCompleted) {
              for (EventInfo eventInfo : eventCompleted) {
            listEvent.put(eventInfo.getEventName(), eventInfo.getEventID());
            } 
        }
     
        return listEvent;
    }

    public void handleEventChange() {
        if (!eventID.equals("0")) {
            List<UserInfo> users = userInfoFacade.getUserByEventID(eventID);
            if (null != users) {
                for (UserInfo userInfo : users) {
                    listWinnerTemp.put(userInfo.getUserName(), userInfo.getUserID());
                }
            } else {
                JsfUtil.addErrorMessage("This Event haven't any player !");
            }
        } else {
            listWinnerTemp = new HashMap<String, String>();
        }
    }

    public String createEvent() {
        try {
            if (newPrize.length() < 10) {
                JsfUtil.addErrorMessage("Prize length must greater than 10 characters");
                return null;
            }
            if (newPrize.length() > 300) {
                JsfUtil.addErrorMessage("Prize length must less than 300 characters");
                return null;
            }
            if (JsfUtil.isSpacesOnly(newPrize.trim())) {
                JsfUtil.addErrorMessage("Please input Prize field.");
                return null;
            }
            if (this.eventID.equals("0")) {
                JsfUtil.addErrorMessage("Please choose Event .");
                return null;
            }
            if (this.playerID.equals("0")) {
                JsfUtil.addErrorMessage("Please choose Player .");
                return null;
            }
            UserInfo userTemp = new UserInfo(playerID);
            EventInfo eventTemp = new EventInfo(Integer.parseInt(eventID));
            Registration reg = registrationFacade.checkTakeIn(userTemp, eventTemp);
            Winner w = new Winner(newPrize, false, reg);
            winnerFacade.create(w);
            reload();
            JsfUtil.addSuccessMessage("Winner was successfully created.");
            return "AddWinnerSuccess";
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, "Fatal  action !");
            return null;
        }
    }

    private void reload() {
        items = null;
        newestWinner = null;
    }

    public String cancel() {
        reload();
        return "CancelAddWinner";
    }

    public String cancelUpdate() {
        reload();
        return "CancelUpdateWinner";
    }

    public String delete() {
        try {
            current = (Winner) getItems().getRowData();
            current.setIsDelete(true);
            winnerFacade.edit(current);
            JsfUtil.addSuccessMessage("Winner was successfully Delete");
            reload();
            return "winner";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, "Error in delete action !");
            return null;
        }
    }

    public String restore() {
        try {
            current = (Winner) getItems().getRowData();
            current.setIsDelete(false);
            winnerFacade.edit(current);
            JsfUtil.addSuccessMessage("Winner was successfully restore");
            reload();
            return "winner";
        } catch (Exception e) {
            System.out.println(e.toString());
            JsfUtil.addErrorMessage(e, "Error in delete action !");
            return null;
        }
    }

    public String prepareEdit() {
        current = (Winner) getItems().getRowData();
        editPlayerID = current.getRegistration().getUserInfo().getUserID();
        editPrize = current.getPrice();
        return "EditWinner";
    }

    public String update() {
        try {
            if (editPrize.length() < 10) {
                JsfUtil.addErrorMessage("Prize length must greater than 10 characters");
                return null;
            }
            if (editPrize.length() > 300) {
                JsfUtil.addErrorMessage("Prize length must less than 300 characters");
                return null;
            }
            if (JsfUtil.isSpacesOnly(editPrize.trim())) {
                JsfUtil.addErrorMessage("Please input Prize field.");
                return null;
            }
            if (this.editPlayerID.equals("0")) {
                JsfUtil.addErrorMessage("Please choose Player .");
                return null;
            }
            UserInfo userTemp = new UserInfo(editPlayerID);
            EventInfo eventTemp = new EventInfo(current.getRegistration().getEventInfo().getEventID());
            Registration reg = registrationFacade.checkTakeIn(userTemp, eventTemp);
            current.setRegistration(reg);
            current.setPrice(editPrize);
            winnerFacade.edit(current);
            reload();
            JsfUtil.addSuccessMessage("Winner was successfully updated.");
            return "UpdateWinnerSuccess";
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, "Fatal Error  action !");
            return null;
        }
    }
}
